#include "stm32f10x.h"
#include "generic.h"
#include "OLED_I2C.h"

const uint8_t Dinosaur1[] = 
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0x80,0x00,0x00,0x00,0x7C,0xF8,0xF0,0xE0,0xE0,0xE0,0xC0,0xC0,0xE0,0xF0,0xFF,0xFF,0xFF,0xFF,0xFC,0x7C,0x3F,0x2F,0x2F,0x2F,0x0F,0x00,0x00,0x00,0x00,0x00,0x03,0x07,0x0F,0x0F,0xFF,0xFF,0x9F,0x0F,0xEF,0xFF,0xBF,0x07,0x03,0x01,0x03,0x00,0x00,0x00,0x00,0x00};/*"C:\Users\benjamin\Desktop\Dinosaur1.bmp",0*/
/* (24 X 24 )*/
const uint8_t Dinosaur2[] = 
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0x80,0x00,0x00,0x00,0x7C,0xF8,0xF0,0xE0,0xE0,0xE0,0xC0,0xC0,0xE0,0xF0,0xFF,0xFF,0xFF,0xFF,0xFC,0x7C,0x3F,0x2F,0x2F,0x2F,0x0F,0x00,0x00,0x00,0x00,0x00,0x03,0x07,0x0F,0x8F,0xFF,0xFF,0x1F,0x1F,0x0F,0x7F,0xFF,0xC7,0x83,0x01,0x03,0x00,0x00,0x00,0x00,0x00};/*"C:\Users\benjamin\Desktop\Dinosaur2.bmp",0*/
/* (24 X 24 )*/
const uint8_t blood[] = 
{0x06,0x0F,0x1F,0x3E,0x1F,0x0F,0x06};/*"C:\Users\benjamin\Desktop\Blood.bmp",0*/
/* (7 X 6 )*/

const uint8_t obstacle1[] = 
{0x18,0x30,0xFF,0xFF,0xF0,0xE0,0x7C,0x03,0x02,0x0F,0x0F,0x0F,0x01,0x00};/*"C:\Users\benjamin\Desktop\obstacle.bmp",0*/
/* (7 X 12 )*/
const uint8_t obstacle2[] = 
{0x7F,0xE0,0xC0,0xFC,0x80,0x80,0xE0,0x00,0x00,0x0F,0x0F,0x0F,0x05,0x00};/*"C:\Users\benjamin\Desktop\obstacle2.bmp",0*/
/* (7 X 12 )*/
const uint8_t obstacle3[] = 
{0x04,0x1E,0x1E,0x1F,0x1B,0x1F,0x1F,0x16,0x14};/*"C:\Users\benjamin\Desktop\obstacle3.bmp",0*/
/* (9 X 5 )*/

const uint8_t Cloud1[] = 
{0x00,0x00,0x3C,0x3C,0x7C,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xF8,0xF8,0xF8,0xF8,0x70,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/*"C:\Users\benjamin\Desktop\Cloud1.bmp",0*/
/* (20 X 10 )*/
const uint8_t Cloud2[] = 
{0x00,0x00,0x38,0x78,0x7C,0xFC,0xFC,0xFC,0xFE,0xFE,0xFE,0xFC,0xFC,0xFC,0x78,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/*"C:\Users\benjamin\Desktop\Cloud2.bmp",0*/
/* (20 X 10 )*/

///////恐龙//////
int DinosaurY = 26;//恐龙的高
uint8_t Dinosaur_blood = 3;//血量
//////障碍物//////
int obstacleX = 130;//障碍物坐标
uint8_t obstacle_index = 0;
///////云朵//////
int16_t CloudX = 150;
uint8_t Cloud_index = 0;

uint32_t score = 0;
void impact(){
	if(obstacleX<=22){
		if(DinosaurY>=14 && Dinosaur_blood>0 && obstacle_index<2){
			obstacleX = 150;
			Dinosaur_blood --;
			obstacle_index ++;
		}
		else if (DinosaurY>=21 && Dinosaur_blood>0 && obstacle_index==2)
		{
			obstacleX = 150;
			Dinosaur_blood --;
			obstacle_index ++;
			OLED_Printf(10,20,6,"!!!");
		}
	}//发生碰撞
	if(Dinosaur_blood==0)//去往极乐世界
		OLED_Printf(40,20,6,"GAME OVER");
}

void jump(){
	static uint8_t jump_flag= 0;//跳跃标志位
	static uint8_t Back_flag= 0;//归为标志位
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)==0 && jump_flag ==0 && Back_flag == 0){
		jump_flag = 1;//跳起来
	}
	if(jump_flag){//允许跳起
		DinosaurY--;
	}
	if(DinosaurY<=0){//限制高度
		jump_flag = 0;
		Back_flag = 1;//允许回落
	}
	if(Back_flag){//回落
		DinosaurY++;
	}
	if(DinosaurY>=26){
		DinosaurY = 26;
		Back_flag = 0;//终止回落
	}
}

int main(void)
{
	uint8_t thread = 0;
	Pin_init(GPIOB,GPIO_Pin_9,GPIO_Mode_IPU);//初始化按键
	OLED_Init(GPIOB,GPIO_Pin_10,GPIO_Pin_11);//STM32和OLED屏幕通讯
	while (1)
	{
		OLED_Clear();

		///////////基础参数///////////
		thread ++;
		if(thread>=8)thread = 0;
		OLED_DrawLine(0,50,128,50);
		OLED_Printf(0,0,6,"score:%d",score);
		score++;

		///////////云朵///////////
		switch (Cloud_index)
		{
		case 0:
			OLED_ShowImage(CloudX,10,20,10,Cloud1);
			break;
		case 1:
			OLED_ShowImage(CloudX,14,20,10,Cloud2);
			break;
		}
		CloudX --;
		if(CloudX<=-20){
			Cloud_index++;
			if(Cloud_index>=2)Cloud_index = 0;
			CloudX=150;
		}

		///////////恐龙///////////
		jump();//跳跃
		impact();//碰撞
		if(thread<=4){
			OLED_ShowImage(0,DinosaurY,24,24,Dinosaur1);
		}else{
			OLED_ShowImage(0,DinosaurY,24,24,Dinosaur2);
		}
		for(uint8_t i = 0;i<Dinosaur_blood;i++){
			OLED_ShowImage(120 - i*8,6,7,6,blood);
		}

		///////////障碍物///////////
		if(obstacleX<=-10){
			obstacleX = 160;//超出屏幕范围，重新生成
			obstacle_index ++;
		}else if(obstacle_index>=3){
			obstacle_index=0;
		}
			
		switch (obstacle_index)
		{
		case 0:
			OLED_ShowImage(obstacleX,38,7,12,obstacle1);
			break;
		case 1:
			OLED_ShowImage(obstacleX,38,7,12,obstacle2);
			break;
		case 2:
			OLED_ShowImage(obstacleX,45,9,5,obstacle3);
			break;
		}
		obstacleX -=2;

		Delay_ms(10);//图像更新速度
		OLED_Update();
	}
}
